package org.marsdonne.audits;

import com.fasterxml.jackson.databind.JsonNode;
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.TypeDef;

import javax.persistence.Column;
import javax.persistence.Entity;
import java.util.Date;

/**
 * 数据变化（审计）域
 * 用于追溯数据导向的变化过程
 */
@Entity
@Getter
@Setter
@NoArgsConstructor
@TypeDef(typeClass = JsonBinaryType.class, defaultForType = JsonNode.class)
public class Change extends LoggedEntity {

    @Column(nullable = false)
    private String serviceId;

    @Column(nullable = false)
    private String domain;

    @Column(nullable = false)
    private Changed.Method method;

    @Column(columnDefinition = "jsonb")
    private JsonNode before;

    @Column(columnDefinition = "jsonb")
    private JsonNode after;

    public Change(Date dateChanged, String serviceId, String domain, Changed.Method method, JsonNode before, JsonNode after) {
        this.dateLogged = dateChanged;
        this.serviceId = serviceId;
        this.domain = domain;
        this.method = method;
        this.before = before;
        this.after = after;
    }

    public Change(Changed changed) {
        this(changed.getDateChanged(), changed.getServiceId(), changed.getDomain(), changed.getMethod(), changed.getBefore(), changed.getAfter());
    }
}
